package com.example.electronicmall.base;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.electronicmall.R;
import com.example.electronicmall.adapter.RoderAdapter;
import com.example.electronicmall.application.MineOrderPresenter;
import com.example.electronicmall.bean.EndMineOrder;
import com.example.electronicmall.bean.MineOrder;
import com.example.electronicmall.bean.OrderItemList;
import com.example.electronicmall.http.IMineOrderPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * 待评价evaluate
 */
public class MineEvaluateActivity extends AppCompatActivity {

    RecyclerView mineEvaluateRV;

    public IMineOrderPresenter mineOrderPresenter;

    OrderItemList itemList;
    EndMineOrder endMineOrder;
    MineOrder order;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_evaluate);

        mineOrderPresenter = new MineOrderPresenter(this);
        //初始化列表
        mineEvaluateRV = findViewById(R.id.mineEvaluateRV);
        mineEvaluateRV.setLayoutManager(new LinearLayoutManager(this));
        initData();
        initFinish();
    }

    private void initFinish() {
        findViewById(R.id.orderEvaluateFinish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {

        mineOrderPresenter.requestOrder(1, new MineOrderPresenter.OnMineOrderListener() {
            @Override
            public void onMineOrder(MineOrder mineOrder) {
                List<Object> listO = new ArrayList<>();
                if (mineOrder.getStatus() == 4) {
                    mineEvaluateRV.setVisibility(View.VISIBLE);
                    List<List<OrderItemList>> orderList = new ArrayList<>();
                    //System.out.println("订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单': " + mineOrder.getStatus());
                    OrderItemList[] orderItemLists = mineOrder.getOrderItemList();
                    List<OrderItemList> listitem = new ArrayList<>();
                    for (int i = 0; i < orderItemLists.length; i++) {
                        itemList = new OrderItemList();
                        itemList.setProductPrice(orderItemLists[i].getProductPrice());
                        itemList.setProductName(orderItemLists[i].getProductName());
                        itemList.setProductPic(orderItemLists[i].getProductPic());
                        listitem.add(itemList);
                    }
                    orderList.add(listitem);

                    //拆分数据
                    if (orderList != null) {
                        for (int i = 0; i < orderList.size(); i++) {
                            order = new MineOrder();
                            order.setOrderSn(mineOrder.getOrderSn());
                            order.setTotalAmount(mineOrder.getTotalAmount());
                            order.setPayAmount(mineOrder.getPayAmount());
                            order.setStatus(mineOrder.getStatus());

                            endMineOrder = new EndMineOrder();
                            endMineOrder.setOrderSn(mineOrder.getOrderSn());
                            endMineOrder.setTotalAmount(mineOrder.getTotalAmount());
                            endMineOrder.setPayAmount(mineOrder.getPayAmount());
                            endMineOrder.setStatus(mineOrder.getStatus());


                            listO.add(order);
                            listO.addAll(orderList.get(i));
                            listO.add(endMineOrder);
                        }
                    }
                    RoderAdapter adapter = new RoderAdapter(listO, MineEvaluateActivity.this);
                    mineEvaluateRV.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                } else {
                    findViewById(R.id.mineEvaluateNullOrder).setVisibility(View.VISIBLE);
                }
            }
        });
    }
}